<template>
  <div class="box">
    <div class="content">
      <div class="left">
        <img
          src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F0G020114924%2F200G0114924-15-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655541070&t=d1a2fa36dd784a6543ea6717cf6f3682"
          alt=""
        />
      </div>
      <div class="right">
        <h3>系统登录</h3>
        <a-form
          :model="formState"
          name="basic"
          :label-col="{ span: 4 }"
          autocomplete="off"
          @finish="onFinish"
        >
          <a-form-item
            label="账户"
            name="name"
            :rules="[
              { required: true, message: 'Please input your username!' },
            ]"
          >
            <a-input v-model:value="formState.name" style="width: 350px" />
          </a-form-item>

          <a-form-item
            label="密码"
            name="pass"
            :rules="[
              { required: true, message: 'Please input your password!' },
            ]"
          >
            <a-input-password
              v-model:value="formState.pass"
              style="width: 350px"
            />
          </a-form-item>

          <a-form-item>
            <a-button type="primary" html-type="submit" block>登录</a-button>
          </a-form-item>
        </a-form>
        <span @click="handle">Or 注册用户</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from "vue";
import { useRouter } from "vue-router";
import { login } from "../../api/login";
interface FormState {
  name: string;
  pass: string;
}
const router = useRouter();
const formState = reactive<FormState>({
  name: "",
  pass: "",
});
const onFinish = async (values: any) => {
  let resp = await login({ ...values });
  let { code, token } = resp.data;
  if (code === 200) {
    router.push("/home");
    localStorage.setItem("token", JSON.stringify(token));
  }
};
const handle = () => {
  router.push("/register");
};
</script>

<style scoped lang="scss">
.box {
  width: 100%;
  height: 100%;
  background: #dddddd;
}
.content {
  width: 950px;
  height: 500px;
  background: white;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
}
.left {
  width: 50%;
  height: 100%;
  img {
    width: 100%;
    height: 100%;
  }
}
.right {
  flex: 1;
  padding: 150px 50px;
}
</style>
